import numpy as np
import struct as st

from models import *

unit_type = np.dtype(  [ \
                   ('L_units', np.str_, 32), \
		           ('M_units', np.str_, 32), \
		           ('T_units', np.str_, 32), \
		           ('V_units', np.str_, 32), \
		           ('L_mks'  , np.float64 ), \
		           ('M_mks'  , np.float64 ), \
		           ('T_mks'  , np.float64 ), \
		           ('V_mks'  , np.float64 )]  )

sim_type  = np.dtype(  [ \
				   ('N massless', np.int64),    \
				   ('N massive' , np.int64),    \
				   ('Sim time'  , np.float64 ), \
				   ('Max dt '   , np.float64 ), \
				   ('Min dt '   , np.float64 ), \
				   ('eta param' , np.float64 ), \
				   ('N snaps'   , np.int64   ), \
				   ('Max dE'    , np.float64 ), \
				   ('GPU'       , np.str_, 4 ), \
				   ('Double'    , np.str_, 4 ), \
				   ('UNITS'     , unit_type  )] )

class SIMULATION:

	def __init__(self, file_name):
		
		print file_name
		
		self.parameters = np.loadtxt( file_name, dtype=sim_type )
		self.models     = []

	def update_Np(self):
	
		model = self.models[-1]
		
		if model.mtype['Status'] == 'on':
			
			if model.mtype['Type'] == 'massive':			
				self.parameters['N massive'] += float( model.mtype['Np'] )
	
			if model.mtype['Type'] == 'massless':
				self.parameters['N massless'] += float( model.mtype['Np'] )
 
	def write_ic_to_binar(self,out_filename):
		
		out = open( out_filename+"_init.bin", 'wb' )
				
		self.parameters.astype(sim_type).tofile(out)
		
		for model in self.models:
			
			N = float(model.mtype['Np'])
			
			model.phcoor.reshape(N*7,1).tofile( out )
			
		out.close()
		
	def add_model( self, m ):
		
		self.models.append(m)
		self.update_Np()
